﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Лабораторная_работа_1_СОД
{
    public partial class Form1 : Form
    {
        public float[,] mas1;
        public float[,] mas2;
        public float[,] mas3;
        bool en1, en2;
        public bool ch1;
        public short ch2 = 1;
        Matrix MyMat,MyMat2,MyMat3;
        
        public Form1()
        {
            InitializeComponent();
           
           
        }
        public string StatusLabel {
            get
            {
                return toolStripStatusLabel1.Text;
            }
            set
            {
                toolStripStatusLabel1.Text = value;
            }
        }
        public bool RowIsNull(int i, int n, int j, float[,] arr)
        {
            for (int j1 = j+1; j1 < n; ++j1)
                if (arr[j1,i] != 0)
                    return false;
            return true;
        }
        public int NumberNotNull(int j, int i, int n, int m,float[,]arr)
        {
            int i1 = i;
            for (int j1 = j; j1 < m; ++j1)
            {
                for (; i1 < n; ++i1)
                    if (arr[i1, j1] != 0)
                        return j1 + 1;
                i1 = 0;
            }
            return 0;
        }
        public bool InitGrid(DataGridView Dgv,int n,int m)
        {
            try
            {
                
                Dgv.RowCount = n;
                Dgv.ColumnCount = m;
                Dgv.VirtualMode = true;
                Dgv.SelectAll();
                Dgv.ClearSelection();
            }
            catch (Exception ex)
            {
                StatusLabel = ex.Message;
                return false;
            }
            return true;
        }
        public void Data1Fill(float[,] arr, int n, int m)
        {
            if(MyMat!=null)
                if ((n < MyMat.Height) || (m < MyMat.Width))
                    InitGrid(dataGridView1, n, m);
            mas1 = new float[n, m];
            
            List<Data> a = new List<Data>();
            Data st=new Data();
            for(int i=0;i<n;++i)
                for (int j = 0; j < m; ++j)
                {
                    //dataGridView1.Rows[i].Cells[j].Value=arr[i,j].ToString();
                    mas1[i, j] = arr[i,j];
                }
            InitGrid(dataGridView1, n, m);
            for (int j = 0; j < m; ++j)
                for (int i = 0; i < n; ++i)
                {
                    if (mas1[i, j] != 0)
                    {
                        st.i = i + 1;
                        st.a = mas1[i, j]; 
                        a.Add(st);
                        if (i == m - 1)
                        {
                            st.i = 0;
                            st.a = NumberNotNull(j+1,0 , n, m,mas1);
                            a.Add(st);
                        }
                    }

                    else 
                    {
                        if (RowIsNull(j,n,i,mas1))
                        {
                            if (a.Count != 0)
                            {
                                if (a[a.Count - 1].i != 0)
                                {
                                    st.i = 0;
                                    st.a = NumberNotNull(j, i, n, m, mas1);
                                    a.Add(st);
                                }
                            }
                            else
                            {
                                st.i = 0;
                                st.a = NumberNotNull(j, i, n, m, mas1);
                                a.Add(st);  
                            }
                        }
                }
            }
            if (a.Count == 0)
            {
                st.i = 0;
                st.a = 0;
                a.Add(st);
            }
            MyMat = new Matrix(n,m,a);
            en1 = true;
            if (en1 && en2)
                командыToolStripMenuItem.Enabled = true;
            SetWatchers1();
        }
        public void Data2Fill(float[,] arr, int n, int m)
        {
            if (MyMat2 != null)
                if ((n < MyMat2.Height) || (m < MyMat2.Width))
                    InitGrid(dataGridView2, n, m);
            mas2 = new float[n, m];
           
            List<Data> a = new List<Data>();
            Data st = new Data();
            for (int i = 0; i < n; ++i)
                for (int j = 0; j < m; ++j)
                {
                    //dataGridView2.Rows[i].Cells[j].Value = arr[i, j].ToString();
                    mas2[i,j] = arr[i,j];
                }
            InitGrid(dataGridView2, n, m);
            for (int j = 0; j < m; ++j)
                for (int i = 0; i < n; ++i)
                {
                    if (mas2[i, j] != 0)
                    {
                        st.i = i + 1;
                        st.a = mas2[i, j];
                        a.Add(st);
                        if (i == m - 1)
                        {
                            st.i = 0;
                            st.a = NumberNotNull(j + 1, 0, n, m, mas2);
                            a.Add(st);
                        }
                    }

                    else
                    {
                        if (RowIsNull(j, n, i, mas2))
                        {
                            if (a.Count != 0)
                            {
                                if (a[a.Count - 1].i != 0)
                                {
                                    st.i = 0;
                                    st.a = NumberNotNull(j, i, n, m, mas2);
                                    a.Add(st);
                                }
                            }
                            else
                            {
                                st.i = 0;
                                st.a = NumberNotNull(j, i, n, m, mas2);
                                a.Add(st);
                            }
                        }
                    }
                }

            if (a.Count == 0)
            {
                st.i = 0;
                st.a = 0;
                a.Add(st);
            }
            MyMat2 = new Matrix(n, m, a);
            en2 = true;
            if (en1 && en2)
                командыToolStripMenuItem.Enabled = true;
            SetWatchers2();
        }
        public void SetWatchers1() {
            Columns1.Text = MyMat.Height.ToString();
            Rows1.Text = MyMat.Width.ToString();
            NotNil1.Text = MyMat.NotNilCout().ToString();
            float d1=MyMat.Width*MyMat.Height;
            float d2=2*(MyMat.NotNilCout()+MyMat.Width+1);
            Comp1.Text = Convert.ToString((d1 / d2 - 1)*100);
            Count1.Text = Convert.ToString(MyMat.Height*MyMat.Width);
            StatusLabel = MyMat.Error;
        }
        public void SetWatchers2()
        {
            Columns2.Text = MyMat2.Height.ToString();
            Rows2.Text = MyMat2.Width.ToString();
            NotNil2.Text = MyMat2.NotNilCout().ToString();
            float d1 = MyMat2.Width * MyMat2.Height;
            float d2 = 2 * (MyMat2.NotNilCout() + MyMat2.Width + 1);
            Comp2.Text = Convert.ToString((d1 / d2 - 1) * 100);
            Count2.Text = Convert.ToString(MyMat2.Height * MyMat2.Width);
            StatusLabel = MyMat2.Error;
        }
        public void SetWatchers3()
        {
            Columns3.Text = MyMat3.Height.ToString();
            Rows3.Text = MyMat3.Width.ToString();
            NotNil3.Text = MyMat3.NotNilCout().ToString();
            float d1 = MyMat3.Width * MyMat3.Height;
            float d2 = 2 * (MyMat3.NotNilCout() + MyMat3.Width + 1);
            Comp3.Text = Convert.ToString((d1 / d2 - 1) * 100);
            Count3.Text = Convert.ToString(MyMat3.Height * MyMat3.Width);
            StatusLabel = MyMat3.Error;
        }
        public void flip(ref float[,]arr,int n,int m)
        {
            float[,] f1=new float[m,n]; 
            for(int i=0;i<n;++i)
                for (int j = 0; j < m; ++j)
                {
                    f1[j, i] = arr[i, j];
                }

            arr = f1;
        }
        private void выходToolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
            
        }

        private void создатьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 Frm2 = new Form2(this);
            Frm2.Show();

        }

        private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            e.Value = mas1[e.RowIndex, e.ColumnIndex];
        }

        private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
        {
            mas1[e.RowIndex, e.ColumnIndex] = Convert.ToSingle(e.Value.ToString());
        }
        private void dataGridView2_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            e.Value = mas2[e.RowIndex, e.ColumnIndex];
        }

        private void dataGridView2_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
        {
            mas2[e.RowIndex,e.ColumnIndex] = Convert.ToSingle(e.Value.ToString());
        }

        private void сложитьМатрицыToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyMat3=MyMat.sum(MyMat2);
            if (MyMat3.Height == 0)
            {
                StatusLabel = "Ошибка! Матрицы разного размера!";
            }
            else
            {
                mas3 = MyMat3.ToArray();
                InitGrid(dataGridView3, MyMat3.Width, MyMat3.Height);
                SetWatchers3();
            }

        }

        private void открытьToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form3 frm3 = new Form3(this);
            frm3.ShowDialog();
            if (!ch1)
            {
                openFileDialog1.InitialDirectory = Application.StartupPath;
                openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    MyMat = new Matrix(openFileDialog1.FileName);
                    mas1 = MyMat.ToArray();
                    flip(ref mas1,MyMat.Width,MyMat.Height);
                    InitGrid(dataGridView1, MyMat.Width, MyMat.Height);
                    SetWatchers1();
                }
                en1 = true;

            }
            else
            { 
                openFileDialog1.InitialDirectory = Application.StartupPath;
                openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    MyMat2 = new Matrix(openFileDialog1.FileName);
                    mas2 = MyMat2.ToArray();
                    flip(ref mas2, MyMat2.Width, MyMat2.Height);
                    InitGrid(dataGridView2, MyMat2.Width, MyMat2.Height);
                    SetWatchers2();
                }
                en2 = true;
            }

            if (en1 && en2)
                командыToolStripMenuItem.Enabled = true;
               
        }

        private void вычестьМатрицыToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyMat3 = MyMat.sub(MyMat2);
            if (MyMat3.Height == 0)
            {
                StatusLabel = "Ошибка! Матрицы разного размера!";
            }
            else
            {
                mas3 = MyMat3.ToArray();
                InitGrid(dataGridView3, MyMat3.Width, MyMat3.Height);
                SetWatchers3();
            }

        }

        private void dataGridView3_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            e.Value = mas3[e.ColumnIndex, e.RowIndex];
        }

        private void dataGridView3_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
        {
            mas3[e.ColumnIndex, e.RowIndex] = Convert.ToSingle(e.Value.ToString());

        }

        private void сравнитьПоЗначениюToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(MyMat2.Similiar(MyMat))
                MessageBox.Show("Матрицы 1 и 2 равны по значению");
            else
                MessageBox.Show("Матрицы 1 и 2 не равны по значению");
                
        }

        private void сохранитьToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form4 frm4 = new Form4(this);
            frm4.ShowDialog();
            if (ch2==1)
            {
                openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFileDialog1.InitialDirectory = Application.StartupPath;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if (MyMat == null)
                    {
                        MessageBox.Show("Матрица ещё не создана!");
                        StatusLabel="Матрица ещё не создана!";
                        return;
                    }
                    MyMat.SaveToFile(saveFileDialog1.FileName);
                }
            }
            if (ch2 == 2)
            {
                saveFileDialog1.InitialDirectory = Application.StartupPath;
                openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if (MyMat2 == null)
                    {
                        MessageBox.Show("Матрица ещё не создана!");
                        StatusLabel="Матрица ещё не создана!";
                        return;
                    }
                    MyMat2.SaveToFile(saveFileDialog1.FileName);
                }
            }
            if(ch2==3)
            { 
                saveFileDialog1.InitialDirectory = Application.StartupPath;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if (MyMat3 == null)
                    {
                        MessageBox.Show("Матрица ещё не создана!");
                        StatusLabel="Матрица ещё не создана!";
                        return;
                    }
                    MyMat3.SaveToFile(saveFileDialog1.FileName);
                }
            } 
          }

        private void button1_Click(object sender, EventArgs e)
        {
            int a;
            int b;
            int c;
            try
            {
                 a= Convert.ToInt32(textBox1.Text);
                 b= Convert.ToInt32(textBox2.Text);
                 c= Convert.ToInt32(textBox3.Text);
            }
            catch (Exception ex)
            {
                a = 1;
                b = 1;
                c = 1;
                MessageBox.Show(ex.Message);
            }
            MyMat.SetElem(a, b, c);
            mas1 = MyMat.ToArray();
            flip(ref mas1, MyMat.Width, MyMat.Height);
            InitGrid(dataGridView1, MyMat.Width, MyMat.Height);
            SetWatchers1();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int a;
            int b;
            int c;
            try
            {
                a = Convert.ToInt32(textBox6.Text);
                b = Convert.ToInt32(textBox5.Text);
                c = Convert.ToInt32(textBox4.Text);
            }
            catch (Exception ex)
            {
                a = 1;
                b = 1;
                c = 1;
                MessageBox.Show(ex.Message);
            }
            MyMat2.SetElem(a, b, c);
            mas2 = MyMat2.ToArray();
            flip(ref mas2, MyMat2.Width, MyMat2.Height);
            InitGrid(dataGridView2, MyMat2.Width, MyMat2.Height);
            SetWatchers2();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int a;
            int b;
            int c;
            try
            {
                a = Convert.ToInt32(textBox9.Text);
                b = Convert.ToInt32(textBox8.Text);
                c = Convert.ToInt32(textBox7.Text);
            }
            catch (Exception ex)
            {
                a = 1;
                b = 1;
                c = 1;
                MessageBox.Show(ex.Message);
            }
            MyMat3.SetElem(a, b, c);
            mas3 = MyMat3.ToArray();
            //flip(ref mas3, MyMat3.Width, MyMat3.Height);
            InitGrid(dataGridView3, MyMat3.Width, MyMat3.Height);
            SetWatchers3();

        }

        private void умножитьМатрицыToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyMat3 = MyMat.mup(MyMat2);
            if (MyMat3.Height == 0)
            {
                StatusLabel = "Ошибка! Матрицы не могут быть перемножены!";
            }
            else
            {
                mas3 = MyMat3.ToArray();
                InitGrid(dataGridView3, MyMat3.Width, MyMat3.Height);
                SetWatchers3();
            }
        }

        private void первуюВоВторуюМатрицуToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyMat.CopyTo(MyMat2);
            mas2 = MyMat2.ToArray();
            flip(ref mas2, MyMat2.Height, MyMat2.Width);
            InitGrid(dataGridView2, MyMat2.Width, MyMat2.Height);
            SetWatchers2();
        }

        private void вторуюВПервуюМатрицуToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyMat2.CopyTo(MyMat);
            mas1 = MyMat.ToArray();
            flip(ref mas1, MyMat.Height, MyMat.Width);
            InitGrid(dataGridView1, MyMat.Width, MyMat.Height);
            SetWatchers1();
        }

        }



}
   

